//
//  ZPRouter_Extensions.swift
//  ZP_URLRouter
//
//  Created by zhangpoor on 17/3/3.
//  Copyright © 2017年 zhangpoor. All rights reserved.
//

import Foundation
import UIKit


//MARK:- < ViewController相关 >
public func viewControllerFromString(_ className: String) -> UIViewController? {
    // get the project name
    
    var obj:UIViewController? =  nil
    
    
    if  let appName: String =  Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as! String?{
        
        //objectForInfoDictionaryKey("CFBundleName") as! String? {
        //拼接控制器名
        let classStringName = "\(appName).\(className)"
        //将控制名转换为类
        let classType = NSClassFromString(classStringName) as? UIViewController.Type
        
        if classType != nil {
            obj = classType?.init()
            
        }
        NSLog("classType:\(classType)")
        /*
         if let type = classType {
         let newVC = type.init()
         return newVC
         }
         */
        
        
    }
    return obj;
}

public extension UIViewController {
    func open(_ url:String) -> Void {
        
        ZP_URLRouter.open(self,
                          url)
    }
    
    
}


//MARK:- < ZPRouter_Tools相关 >
public func getStringAfterChar(_ tString:String ,  _ tChar:Character) -> String? {
    
    var result:String?

    for item in tString.characters.indices {
        
        if tString[item] == tChar
        {
            
            if item == tString.endIndex  {
                result = tString.substring(from: tString.index(after: item))
            }
            else{
                result = ""
            }
            break
        }
    }
    
    return result
}

public func getStringBeforeChar(_ tString:String ,  _ tChar:Character) -> String? {
    
    var result:String?

    for item in tString.characters.indices {
        
        if tString[item] == tChar
        {
            
            if item != tString.endIndex  {
                result = tString.substring(to: item)
            }

            break
        }
    }
    
    return result
}
